package com.site.wisdom_construction.controller.personnelModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.Server.WebSocketServer;
import com.site.wisdom_construction.entity.personnelModule.ContractWorkers;
import com.site.wisdom_construction.entity.personnelModule.ManagerialStaff;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.personnelModule.ManagerialStaffService;
import com.site.wisdom_construction.util.TimeUtils;
import com.site.wisdom_construction.util.imgUtil.AliyunOSSUtil;
import com.site.wisdom_construction.util.imgUtil.OssToBase64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** @Author daiyifei @Date 2022/1/4 @Description */
@Api(tags = "管理人员接口")
@RestController
@RequestMapping("ManagerialStaff")
@CrossOrigin
public class ManagerialStaffController {
  @Autowired private ManagerialStaffService managerialStaffService;

  @ApiOperation(value = "下发数据", notes = "测试数据:")
  @PostMapping("/distributedData")
  public JsonResult distributedData(@RequestBody Map map) throws IOException {
    JsonResult jsonResult = new JsonResult();
    OssToBase64 ossToBase64 = new OssToBase64();
    List<Integer> list = (List<Integer>) map.get("list");
    Map mapR = new HashMap();
    int l = 0;
    for (int i = 0; i < list.size(); i++) {
      mapR.put("id", list.get(i));
      ManagerialStaff managerialStaff = managerialStaffService.findManagerialStaffById(mapR);
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("cmd", "upload person");
      jsonObject.put("id", managerialStaff.getManagerialCard());
      jsonObject.put("name", managerialStaff.getManagerialName());
      jsonObject.put("role", 1);
      jsonObject.put("kind", 0);
      jsonObject.put("term_start", "2021/11/10 12:00:00");
      jsonObject.put("term", "2032/12/18 16:45:30");
      jsonObject.put("reg_image", ossToBase64.getBase64(managerialStaff.getManagerialPicture()));
      WebSocketServer webSocketServer = new WebSocketServer();
      webSocketServer.BroadCastInfo(jsonObject.toJSONString());
      l = l+1;
    }
    if (l >= 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功下发人员信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("下发人员信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "增加管理人员信息", notes = "测试数据:")
  @PostMapping("/insertLabourEnterpriseApproach")
  public JsonResult insertManagerialStaff(@RequestBody Map map) throws IOException {
    JsonResult jsonResult = new JsonResult();
    JSONObject jsonObject = new JSONObject();
    map.put("entryDate", TimeUtils.getNowTime());
    if (map.get("picture") != null) {
      // 获取现场拍照图片,上传到oss上
      File newFile = new File("/root/personImg/" + (String) map.get("picture"));
      if (newFile.isFile()) {
        jsonResult.setCode(20006);
        jsonResult.setMessage("拍照信息有误重新录入!");
      }
      String Host = "faceUpload";
      // 上传到OSS
      String uploadUrl = AliyunOSSUtil.upload(newFile, Host);
      map.put("managerialPicture", uploadUrl);

      byte[] data = null;
      try (InputStream in = new FileInputStream("/root/personImg/" + (String) map.get("picture"))) {
        data = new byte[in.available()];
        in.read(data);
      } catch (IOException e) {
        e.printStackTrace();
      }
      jsonObject.put("cmd", "upload person");
      jsonObject.put("id", map.get("workerCard"));
      jsonObject.put("name", map.get("workerName"));
      jsonObject.put("role", 1);
      jsonObject.put("kind", 0);
      jsonObject.put("term_start", "2021/11/10 12:00:00");
      jsonObject.put("term", "2032/12/18 16:45:30");
      jsonObject.put("reg_image", Base64.getEncoder().encodeToString(data));
      // 删除服务器缓存的图片
      newFile.delete();
    }
    List<ManagerialStaff> managerialStaffList =
        managerialStaffService.findManagerialStaffByPhone(map);
    if (managerialStaffList.size() > 0) {
      jsonResult.setCode(200);
      jsonResult.setMessage("管理人员已存在!");
    } else {
      int i = managerialStaffService.insertManagerialStaff(map);
      if (i == 1) {
        if (map.get("picture") != null) {
          // 下发数据到设备
          WebSocketServer webSocketServer = new WebSocketServer();
          webSocketServer.BroadCastInfo(jsonObject.toJSONString());
        }

        jsonResult.setCode(200);
        jsonResult.setMessage("成功录入管理人员信息!");
      } else {
        jsonResult.setCode(20006);
        jsonResult.setMessage("录入管理人员信息失败!");
      }
    }
    return jsonResult;
  }

  @ApiOperation(value = "删除管理人员信息", notes = "测试数据:")
  @PostMapping("/deleteManagerialStaff")
  public JsonResult deleteManagerialStaff(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    List<Integer> list = (List<Integer>) map.get("list");
    int l = 0;
    Map mapR = new HashMap();
    for (int i = 0; i < list.size(); i++) {
      mapR.put("id", list.get(i));
      int a = managerialStaffService.deleteManagerialStaff(mapR);
      l += a;
    }
    if (l >= 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功删除管理人员信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("删除管理人员信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "修改管理人员信息", notes = "测试数据:")
  @PostMapping("/updateManagerialStaff")
  public JsonResult updateManagerialStaff(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    int i = managerialStaffService.updateManagerialStaff(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功修改管理人员信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("修改管理人员信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "管理人员进场", notes = "测试数据:")
  @PostMapping("/updateManagerialStaffByStatusJ")
  public JsonResult updateManagerialStaffByStatusJ(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    map.put("entryDate", TimeUtils.getNowTime());
    int i = managerialStaffService.updateManagerialStaffByStatusJ(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("管理人员进场成功!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("管理人员进场失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "管理人员离场", notes = "测试数据:")
  @PostMapping("/updateManagerialStaffByStatusT")
  public JsonResult updateManagerialStaffByStatusT(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    map.put("leaveDate", TimeUtils.getNowTime());
    int i = managerialStaffService.updateManagerialStaffByStatusT(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("管理人员离场成功!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("管理人员离场失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "分页模糊查询管理人员信息", notes = "测试数据:")
  @PostMapping("/findManagerialStaffByPage")
  public Page findManagerialStaffByPage(@RequestBody Map map) {
    Page page = new Page();
    page.setPageNo((Integer) map.get("pageNo"));
    page.setPageSize((Integer) map.get("pageSize"));
    page.setTotal(managerialStaffService.Total(map));
    page.setItems(managerialStaffService.findManagerialStaffByPage(map));
    return page;
  }

  @ApiOperation(value = "根据id查询管理人员信息", notes = "测试数据:")
  @PostMapping("/findManagerialStaffById")
  public OneDataResult findManagerialStaffById(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(managerialStaffService.findManagerialStaffById(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据项目id查询所有安管人员", notes = "测试数据:")
  @PostMapping("/findManagerialStaffByProjectId")
  public OneDataResult findManagerialStaffByProjectId(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(managerialStaffService.findManagerialStaffByProjectId(map));
    return oneDataResult;
  }
}
